package com.zhuangwei.spring.hook.interceptor;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器，我们要求必须传入referer
 *      referer：就是在请求头中，代表了你请求的资源来自哪个服务器的标志
 *      用途广泛：比如，百度流量统计，当然也要反盗功能的
 *
 * 测试方法：
 *
 * @author 文希
 * @create 2019-05-23 14:48
 **/
@ComponentScan
public class MyRefererInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------------------in My Referer Interceptor-----------------------------");
        String referer = request.getParameter("referer");
        if (StringUtils.isEmpty(referer) || !"http://hhui.top".equals(referer)){
            return false;
        }
        return true;
    }
}
